package com.xdwanj.dao.impl;

import com.mysql.jdbc.StringUtils;
import com.xdwanj.dao.BaseDao;
import com.xdwanj.dao.UserDao;
import com.xdwanj.pojo.Pet;
import com.xdwanj.pojo.User;
import com.xdwanj.util.PageBean;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

public class UserDaoImpl implements UserDao {
    private BaseDao baseDao = new BaseDao();
    JdbcTemplate jdbcTemplate = baseDao.getJdbcTemplate();

    @Override
    public PageBean findAll(int pageSize, int currentPageNo) throws Exception {
        String sql = "select * from user";
        String sqlNum = "select count(*) from user";
        PageBean customerPageDto = baseDao.getCustomerPageDto(sql, null, new BeanPropertyRowMapper<User>(User.class), sqlNum, null, currentPageNo, pageSize);
        return customerPageDto;
    }

    @Override
    public PageBean<User> findUserListByName(String name, int pageSize, int currentPageNo) throws Exception {

        if (name == null) {
            name = "";
        }

        name = name.trim();
        StringBuffer Name = new StringBuffer(name);
        for (int i = 0; i < name.length(); i++) {
            if (name.charAt(i) == ' ') {
                Name.setCharAt(i, '%');
            }
        }
        name = Name.toString();
        String sql = "select * from user where user.name LIKE '%" + name + "%'";
        String sqlNum = "select count(*) from user where user.name LIKE '%" + name + "%'";
        PageBean customerPageDto = baseDao.getCustomerPageDto(sql, null, new BeanPropertyRowMapper<User>(User.class), sqlNum, null, currentPageNo, pageSize);
        List<User> users = customerPageDto.getContent();
        Collections.sort(users, new Comparator<User>() {
            @Override
            public int compare(User name1, User name2) {
                int result = name1.getName().length() - name2.getName().length();
                return result;
            }
        });
        return customerPageDto;
    }

    @Override
    public User findUserByName(String name) throws Exception {
        name = name.trim();
        String sql = "select * from user where user.name = ?";
        User user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), name);
        return user;
    }

    @Override
    public User findUserByUserNameAndPassword(String userName, String password) throws Exception {
        String sql = "select * from user u where u.username = ? and u.password = ?";
        User user = null;
        user = (User) jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), userName, password);
        return user;
    }


    @Override
    public int addUser(User user) throws Exception {
        String name = null;

        if (user != null && !StringUtils.isNullOrEmpty(user.getName())) {
            name = user.getName();
        } else {
            name = user.getUsername();
        }
        Date date = new Date();
        String sql = "insert into user (name,username,password,email,phone_num,register_date) values(?,?,?,?,?,?)";
        int addRow = jdbcTemplate.update(sql, name, user.getUsername(), user.getPassword(), user.getEmail(), user.getPhoneNum(), date);
        return addRow;
    }

    @Override
    public int addUserAll(User user) throws Exception {
        String name = null;

        if (user != null && !StringUtils.isNullOrEmpty(user.getName())) {
            name = user.getName();
        } else {
            name = user.getUsername();
        }
        String sql = "insert into user (name,username,password,email,phone_num,register_date,sex,address,birthday,image) values(?,?,?,?,?,?,?,?,?,?)";
        int addRow = jdbcTemplate.update(sql, name, user.getUsername(), user.getPassword(), user.getEmail(), user.getPhoneNum(), user.getRegisterDate(), user.getSex(), user.getAddress(), user.getBirthday(), user.getImage());
        return addRow;
    }


    @Override
    public int updateUser(User user) throws Exception {
        String sql = "update user set name = ?,username = ?,password = ?,sex = ?,email = ?,phone_num = ?,register_date = ?,address = ?,birthday = ?,image = ? where id = ?";
        int i = jdbcTemplate.update(sql, user.getName(), user.getUsername(), user.getPassword(), user.getSex(), user.getEmail(), user.getPhoneNum(), user.getRegisterDate(), user.getAddress(), user.getBirthday(), user.getImage(), user.getId());
        return i;
    }

    @Override
    public int updatePassWord(int id, String NewPassword) throws Exception {
        String sql = "update user set password = ? where id = ?";
        int i = jdbcTemplate.update(sql, NewPassword, id);
        return i;
    }


    @Override
    public int deleteUser(int id) throws Exception {
        String userSql = "delete from user where id = ?";
        String shoppingCartSql = "delete from shopping_cart where U_id = ?";
        String user_shopping_cartSql = "delete from user_shopping_cart where u_id = ?";
        String orderSql = "delete from orders where u_id = ?";
        String user_ordersSql = "delete from user_orders where u_id = ?";
        String petSql = "delete from pet where u_id = ?";
        String user_petSql = "delete from user_pet where u_id = ?";
        int delRow = jdbcTemplate.update(userSql, id);
        delRow = delRow + jdbcTemplate.update(shoppingCartSql, id);
        delRow = delRow + jdbcTemplate.update(user_shopping_cartSql, id);
        delRow = delRow + jdbcTemplate.update(orderSql, id);
        delRow = delRow + jdbcTemplate.update(user_ordersSql, id);
        delRow = delRow + jdbcTemplate.update(petSql, id);
        delRow = delRow + jdbcTemplate.update(user_petSql, id);
        return delRow;
    }

    @Override
    public PageBean<Pet> findPetListById(int id, int pageSize, int currentPageNo) throws Exception {
        StringBuffer sql = new StringBuffer(" from user u,pet p,user_pet up where u.id = up.u_id and p.id = up.p_id and u.id = ?");

        PageBean<Pet> customerPageDto = baseDao.getCustomerPageDto(
                new StringBuffer(" select * ").append(sql).toString(),
                new Object[]{id},
                new BeanPropertyRowMapper<Pet>(Pet.class),
                new StringBuffer(" select count(*) ").append(sql).toString(),
                new Object[]{id},
                currentPageNo,
                pageSize);

        return customerPageDto;
    }

    @Override
    public User findUserById(int id) throws Exception {
        String sql = "select * from user u where u.id = ?";
        User user = new User();
        user = (User) jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), id);
        return user;
    }

}
